package com.bayi.storage.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by guoping on 16/10/11.
 */
public class DateUtil {

    public static class DateDetail {
        public int year;
        public int month;
        public int day;
        public int dayOfWeak;
        public int hour;
        public int minute;
        public int second;
    }

    public static String toString(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 获取当前时间字符串
     * <p>格式为用户自定义</p>
     *
     * @param format 时间格式
     * @return 时间字符串
     */
    public static String getCurTimeString(String format) {
        return date2String(new Date(), format);
    }

    /**
     * @param strDate yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date parse(String strDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(strDate);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public static long dateDiff(String startTime, String endTime, String format) {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long day = 0;
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime()
                    - sd.parse(startTime).getTime();
            day = diff / nd;// 计算差多少天
            long hour = diff % nd / nh;// 计算差多少小时
            long min = diff % nd % nh / nm;// 计算差多少分钟
            long sec = diff % nd % nh % nm / ns;// 计算差多少秒
            // 输出结果
            System.out.println("时间相差：" + day + "天" + hour + "小时" + min
                    + "分钟" + sec + "秒。");
//            if (day>=1) {
            return day;
//            }else {
//                if (day==0) {
//                    return 0;
//                }else {
//                    return 0;
//                }

//            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day;

    }

    /**
     * 将Date类型转为时间字符串
     * <p>格式为用户自定义</p>
     *
     * @param time   Date类型时间
     * @param format 时间格式
     * @return 时间字符串
     */
    public static String date2String(Date time, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
        return simpleDateFormat.format(time);
    }

    public static DateDetail getDetail(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        DateDetail detail = new DateDetail();
        detail.year = calendar.get(Calendar.YEAR);
        detail.month = calendar.get(Calendar.MONTH);
        detail.day = calendar.get(Calendar.DAY_OF_MONTH);
        detail.dayOfWeak = calendar.get(Calendar.DAY_OF_WEEK);
        detail.hour = calendar.get(Calendar.HOUR_OF_DAY);
        detail.minute = calendar.get(Calendar.MINUTE);
        detail.second = calendar.get(Calendar.SECOND);
        return detail;
    }

    public static Date getMonthFirstDay(Date date) {
        DateDetail detail = getDetail(date);
        String strDate = detail.year + "-" +
                StringUtil.padLeft(String.valueOf(detail.month + 1), 2, '0') + "-01 00:00:00";
        return parse(strDate);
    }

    public static Date getWeakFirstTime(Date date) {
        DateDetail detail = getDetail(date);
        return getDate(addDay(date, -(detail.dayOfWeak - 1)));
    }

    //时分秒归0
    public static Date getDate(Date date) {
        DateDetail detail = getDetail(date);
        String strDate = detail.year + "-" +
                StringUtil.padLeft(String.valueOf(detail.month + 1), 2, '0') + "-" +
                StringUtil.padLeft(String.valueOf(detail.day), 2, '0') + " 00:00:00";

        return parse(strDate);
    }

    //加减月数
    public static Date addMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date.getTime());
        calendar.add(Calendar.MONTH, month);
        return calendar.getTime();
    }

    //加减天数
    public static Date addDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date.getTime());
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return calendar.getTime();
    }

    //加减小时
    public static Date addHour(Date date, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date.getTime());
        calendar.add(Calendar.HOUR_OF_DAY, hour);
        return calendar.getTime();
    }

    public static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

    public static Date stringToDate(String time, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);//小写的mm表示的是分钟  
        try {
            return sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    /**
     * 根据时间戳来判断当前的时间是几天前,几分钟,刚刚
     *
     * @param dateTime
     * @return
     */
    public static String getTimeStateNew(String dateTime) {

        Date dtTime = parse(dateTime);
        String strTime = String.valueOf(dtTime.getTime());
        String long_by_13 = "1000000000000";
        String long_by_10 = "1000000000";
        if (Long.valueOf(strTime) / Long.valueOf(long_by_13) < 1) {
            if (Long.valueOf(strTime) / Long.valueOf(long_by_10) >= 1) {
                strTime = strTime + "000";
            }
        }
        Timestamp time = new Timestamp(dtTime.getTime());
        Timestamp now = new Timestamp(System.currentTimeMillis());
        long day_conver = 1000 * 60 * 60 * 24;
        long hour_conver = 1000 * 60 * 60;
        long min_conver = 1000 * 60;
        long time_conver = now.getTime() - time.getTime();
        long temp_conver;
        if ((time_conver / day_conver) < 2) {
            temp_conver = time_conver / day_conver;
            if (temp_conver == 1) {
                if (IsYesterday(dateTime)) {
                    return "昨天" + toString(dtTime, "HH:mm");
                } else {
                    return toString(dtTime, "MM-dd HH:mm");
                }
            } else {
                temp_conver = (time_conver / hour_conver);
                if (temp_conver >= 1) {
                    if (IsToday(dateTime)) {
                        return "今天" + toString(dtTime, "HH:mm");
                    } else {
                        return "昨天" + toString(dtTime, "HH:mm");
                    }
                } else {
                    temp_conver = (time_conver / min_conver);
                    if (temp_conver >= 1) {
                        return temp_conver + "分钟前";
                    } else {
                        return "刚刚";
                    }
                }
            }
        } else {
            return IsToYear(dtTime);
        }
    }

    /**
     * 毫秒转时分秒
     *
     * @param hoursStr
     * @param minutesStr
     * @param lSeconds
     * @return
     */
    public static String convertSecToTimeString(String hoursStr, String minutesStr, long lSeconds) {
        int totalSeconds = (int) (lSeconds / 1000);
        int seconds = totalSeconds % 60;//秒
        int minutes = (totalSeconds / 60) % 60;//分
        int hours = totalSeconds / 3600;//时

        return hours > 0 ? String.format(hoursStr, hours, minutes, seconds) : String.format(minutesStr, minutes, seconds);
    }

    public static String transfom(long time) {
        long shi = time / 3600;
        long fen = (time % 3600) / 60;
        long miao = (time % 3600) % 60;
        //shi< 10 ? ("0" + shi) : shi)判断时否大于10时的话就执行shi,否则执行括号中的
        return (shi < 10 ? ("0" + shi) : shi) + ":" + (fen < 10 ? ("0" + fen) : fen) + ":" + (miao < 10 ? ("0" + miao) : miao);
    }

    /**
     * 判断是否为今年(效率比较高)
     *
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     */
    /**
     * 判断是否为今年(效率比较高)
     *
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     */
    public static String IsToYear(Date day) {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(day);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            return toString(day, "MM-dd HH:mm");
        }
        return toString(day, "yyyy-MM-dd HH:mm");
    }

    /**
     * 判断是否为今天(效率比较高)
     *
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean IsToday(String day) {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        Date date = parse(day);
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);
            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 时间戳转换成字符窜
     *
     * @param milSecond
     * @param pattern
     * @return
     */
    public static String getDateToString(long milSecond, String pattern) {
        Date date = new Date(milSecond);
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 判断是否为昨天(效率比较高)
     *
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean IsYesterday(String day) {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = parse(day);
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);
            if (diffDay == -1) {
                return true;
            }
        }
        return false;
    }

    /**
     * <p>
     * Des: 包含年月日带时、分、秒的时间格式，时间为24小时制的
     */
    public static final String DEFAULT_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 得到当前时间string类型
     */
    public static String getStrTime()  {
        SimpleDateFormat sdf=new SimpleDateFormat(DEFAULT_TIME_FORMAT,Locale.CHINA);
        String time=null;
        try {
            time= sdf.format(new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  time;
    }
}
